#! /usr/bin/python
from ctypes import *

dll_loader = cdll
mod_name = "libtwofish.so"
twofish_mod = dll_loader.LoadLibrary(mod_name)
BLOCK_SIZE = 16
MAX_KEY_LEN = 32
EXPANDED_KEY_SIZE = 160

class key_type(Structure):
	__fileds__ = [   ("key_size", c_int)
					,("orignal_key", c_ubyte * MAX_KEY_LEN)
					,("expanded_key", c_uint * (EXPANDED_KEY_SIZE / 4))
					,("s_array", c_uint * (MAX_KEY_LEN/4))]

def pre_expand_key(original_key, key_size):
	c_original_key = c_char_p(original_key)
	c_key_size = c_int(key_size)
	my_key = key_type()
	if twofish_mod.pre_expand_key(c_original_key, c_key_size, addressof(my_key)) != 0:
		print "expand key error!"
		return None
	return my_key

def twofish_128_encrypt(plaintext, encrypt_key):
	c_plaintext = c_char_p(plaintext)
	c_cipher = create_string_buffer(BLOCK_SIZE)
	twofish_mod.twofish_128_encrypt(c_plaintext, c_cipher, addressof(encrypt_key))
	return c_cipher.value

